package com.zhida.jiyangcenter.account

import android.os.Build
import android.os.Bundle
import android.view.View
import androidx.lifecycle.lifecycleScope
import chooongg.base.annotation.ContentLayout
import chooongg.base.annotation.TitleBar
import chooongg.base.manager.app
import chooongg.base.utils.*
import chooongg.http.APIException
import chooongg.http.HttpCallback
import com.zhida.jiyangcenter.R
import com.zhida.jiyangcenter.api.APIAccount
import com.zhida.jiyangcenter.api.BaseAPI
import com.zhida.jiyangcenter.api.LoginEntity
import com.zhida.jiyangcenter.api.NetResponse
import com.zhida.jiyangcenter.mian.BaseActivity
import com.zhida.jiyangcenter.mian.MainActivity
import com.zhida.jiyangcenter.utils.SP
import com.zhida.jiyangcenter.utils.request
import kotlinx.android.synthetic.main.activity_forget_code.*
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.delay
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext

@TitleBar(false)
@ContentLayout(R.layout.activity_forget_code)
class ForgetCodeActivity : BaseActivity() {
    override fun initConfig(savedInstanceState: Bundle?) {
        val window = activity.window
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M)
            window.decorView.systemUiVisibility =
                View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN or View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR
        title_bar.navigationIcon(R.drawable.iv_back_fill) {
            finish()
        }
        tv_code_phone.text = "已发送验证码至 +86 ${intent.getStringExtra("phone")}"
        tv_login_code_agent.setOnClickListener {
            lifecycleScope.launch {
                val s = APIAccount.service().sendCode(intent.getStringExtra("phone"))
                logE(s)
                authCodeCountDown()
            }
        }
        lifecycleScope.launch {
            authCodeCountDown()
        }
        ecv_code.setEditCodeListener {
            lifecycleScope.request({
                APIAccount.service().login(intent.getStringExtra("phone") ?: "", 0, code = it)
            }, object : BaseAPI<LoginEntity>() {

                override fun onError(e: APIException) {
                    tipFail(e.eMessage)
                }

                override fun onSuccess(loginEntity: LoginEntity?, state: Int, msg: String) {
                    SP.isLogin.put(false)
                    SP.key_id.put(loginEntity?.key_id)
                    SP.account.put(loginEntity?.account)
                    SP.avatar.put(loginEntity?.avatar)
                    SP.EBusinessID.put(loginEntity?.business_id)
                    SP.AppKey.put(loginEntity?.app_key)
                    SP.ReqURL.put(loginEntity?.req_url)
                    startActivity<ForgetPassActivity> { }
                    finish()
                }

            })
        }

    }

    /**
     * 发送验证码后的倒计时
     */
    private suspend fun authCodeCountDown() {
        var time = 61
        while (time > 0 && !isFinishing) {
            time--
            if (time > 0) {
                withContext(Dispatchers.Main) {
                    tv_login_code_agent.isClickable = false
                    tv_login_code_agent.text = resString(R.string.login_been_send_auth_code, time)
                }
            } else {
                withContext(Dispatchers.Main) {
                    tv_login_code_agent.setText(R.string.login_get_auth_code_retry)
                    tv_login_code_agent.isClickable = true
                }
            }
            delay(1000)
        }
    }
}